<template>
    <div class="screen-view">

      <div class="screen-item">
        <span class="screen-title">患者姓名</span>
        <el-input v-model="data.name" placeholder="请输入患者姓名" @keyup.native.enter="enterClick"></el-input>
      </div>


      <div class="screen-item">
        <span class="screen-title">选择医生</span>
        <el-select v-model="data.doc" placeholder="请选择医生" @change="docChange">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.real_name"
            :value="item.id">
          </el-option>
        </el-select>
      </div>

      <div class="screen-item">
        <span class="screen-title">选择时间</span>
        <el-date-picker
          v-model="value"
          type="daterange"
          align="right"
          unlink-panels
          :clearable="clearable"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
          :picker-options="pickerOptions">
        </el-date-picker>
      </div>

      <el-button class="main-btn" type="primary" @click="searchClick">查询</el-button>
      <el-button class="border-btn" @click="clearData">重置</el-button>
      <el-button class="border-btn" @click="refreshData">刷新</el-button>

    </div>
</template>

<script>
  import {getDoctorment} from "../../../api/registration";

  export default {
        name: "screenView",
        props: {
            data:Object
        },
        model: {
            prop: 'data',
            event: 'change'
        },
        data:function () {
            return{
                clearable:false,
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }],
                    disabledDate(time) {
                        if (time.getTime() < Date.now()){
                            return false;
                        }else {
                            return true;
                        }

                        // return time.getTime() > Date.now();
                    }
                },
                value:[],
                options:[],
            }
        },
        mounted:function(){
            this.value = [new Date(),new Date()]
            this.getDoctorment();
        },
        methods:{
            dateChange:function (val) {
                this.data.startDate = this.dealDate(this.value[0]) + " 00:00:00";
                this.data.endDate = this.dealDate(this.value[1]) + " 23:59:59"

                this.$emit("get-data");
            },
            docChange:function(){
                this.$emit("get-data");
            },
            enterClick:function(){
                this.$emit("get-data");
            },
            searchClick:function(){
                this.$emit("get-data");
            },
            clearData:function(){
                this.value = [new Date(),new Date()]
                this.data.startDate = this.dealDate(this.value[0]) + " 00:00:00";
                this.data.endDate = this.dealDate(this.value[1]) + " 23:59:59"
                this.data.name = '';
                if (this.options.length>0){
                    this.data.doc = 0;
                }

                this.$emit("get-data");
            },
            dealDate:function (date) {
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                if (month < 10){
                    month = "0" + month
                }
                if (day < 10){
                    day = "0" + day;
                }

                return year + ":" + month + ":" + day
            },
            getDoctorment:function(){
                getDoctorment().then((res)=>{
                    if (res.code == 200){
                        this.options = res.data;
                        if (this.options.length > 0){
                            this.options.unshift({real_name:"全部",id:0})
                        }
                    }
                })
            },
            refreshData:function () {
                this.$emit("get-data");
            },
        }
    }
</script>

<style lang="scss" scoped>
  @import "@/assets/scss/payOrDrug/screenView.scss";
</style>
